package com.csun.nusing.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.RadioGroup;

import com.csun.nusing.R;
import com.csun.nusing.presenter.NursingMainPresenter;
import com.csun.nusing.presenter.inter.INursingMain;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.fragment.NursingAlarmFragment;
import com.csun.nusing.ui.fragment.NursingHomeFragment;
import com.csun.nusing.ui.fragment.NursingMineFragment;
import com.csun.nusing.ui.fragment.NursingOldsterFragment;
import com.csun.nusing.ui.jpush.TagAliasOperatorHelper;
import com.csun.nusing.ui.jpush.TagAliasOperatorHelper.TagAliasBean;
import com.csun.nusing.util.SPUtils;

import java.util.LinkedHashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * 护工界面的主界面 底部4个tabbar 点击切换界面显示 ClassName: NursingMainActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-19
 */
public class NursingMainActivity extends BaseActivity<NursingMainPresenter>
		implements INursingMain {

	private FragmentManager fragmentManager;
	private RadioGroup bottomrg;
	private NursingHomeFragment nursingHomeFragment;
	private NursingAlarmFragment nursingAlarmFragment;
	private NursingOldsterFragment nursingOldsterFragment;
	private NursingMineFragment nursingMineFragment;
	// for receive customer msg from jpush server
	private MessageReceiver mMessageReceiver;
	public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
	public static final String KEY_TITLE = "title";
	public static final String KEY_MESSAGE = "message";
	public static final String KEY_EXTRAS = "extras";
	public static boolean isForeground = false;

	@Override
	public int getContentViewId() {
		return R.layout.activity_nursing_main;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		fragmentManager = this.getSupportFragmentManager();
		bottomrg = findViewById(R.id.activity_nursingmain_bottom_rg);
		initFragment();
		JPushInterface.init(getApplicationContext());
		registerMessageReceiver(); // used for receive msg
	}

	private void initFragment() {
		nursingHomeFragment = NursingHomeFragment.newInstance();
		nursingAlarmFragment = NursingAlarmFragment.newInstance();
		nursingOldsterFragment = NursingOldsterFragment.newInstance();
		nursingMineFragment = NursingMineFragment.newInstance();
	}

	@Override
	public void initData() {
		super.initData();
		mPresenter.controlRadioGroup(bottomrg);
		addTag();

	}

	/**
	 * 获取设置tags
	 * */
	private void addTag() {
		String tag = (String) SPUtils.get(this, "userId", "10001");
		// 检查 tag 的有效性
		Set<String> tagSet = new LinkedHashSet<String>();
		tagSet.add(tag);
		// ","隔开的多个 转换成 Set
		TagAliasBean tagAliasBean = new TagAliasBean();
		tagAliasBean.action = TagAliasOperatorHelper.ACTION_ADD;
		TagAliasOperatorHelper.sequence++;
		tagAliasBean.tags = tagSet;
		TagAliasOperatorHelper.getInstance().handleAction(
				getApplicationContext(), TagAliasOperatorHelper.sequence,
				tagAliasBean);

	}

	@Override
	protected NursingMainPresenter getPresenter() {
		return new NursingMainPresenter(this);
	}

	/**
	 * 底部tabbar的点击事件的回调
	 */
	@Override
	public void check(int rbFlag) {
		switch (rbFlag) {
		case 0:
			mPresenter.controlFragment(fragmentManager, nursingHomeFragment,
					"nursingHomeFragment");
			break;
		case 1:
			mPresenter.controlFragment(fragmentManager, nursingAlarmFragment,
					"nursingAlarmFragment");
			break;
		case 2:
			mPresenter.controlFragment(fragmentManager, nursingOldsterFragment,
					"nursingOldsterFragment");
			break;
		case 3:
			mPresenter.controlFragment(fragmentManager, nursingMineFragment,
					"nursingMineFragment");
			break;
		default:
			break;
		}
	}

	/**
	 * jpsh接受器
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-6
	 */
	public void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();
		filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
		filter.addAction(MESSAGE_RECEIVED_ACTION);
		LocalBroadcastManager.getInstance(this).registerReceiver(
				mMessageReceiver, filter);
	}

	/**
	 * 接收消息 ClassName: MessageReceiver
	 * 
	 * @Description: TODO
	 * @author fzq
	 * @date 2017-11-3
	 */
	public class MessageReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
					String messge = intent.getStringExtra(KEY_MESSAGE);
					String extras = intent.getStringExtra(KEY_EXTRAS);
					StringBuilder showMsg = new StringBuilder();
					showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
						showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
				}
			} catch (Exception e) {
			}
		}
	}

	@Override
	protected void onResume() {
		isForeground = true;
		super.onResume();
	}

	@Override
	protected void onPause() {
		isForeground = false;
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		LocalBroadcastManager.getInstance(this).unregisterReceiver(
				mMessageReceiver);
	}

}
